package be.sc.classicnetwork.service.impl;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import be.sc.classicnetwork.exception.ApplicationException;
import be.sc.classicnetwork.service.MailService;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;

/**
 * MailServiceImpl.
 */
public class MailServiceImpl implements MailService {

	private static Logger logger = Logger.getLogger(MailServiceImpl.class);

	private JavaMailSender mailSender;

	private Configuration configuration;

	/**
	 * @see be.sc.classicnetwork.service.MailService#send(java.lang.String,
	 *      java.lang.String, java.lang.String, java.lang.String,
	 *      java.lang.String, java.util.Collection)
	 */
	public void send(final String from, final String fromPersonal,
			final String to, final String subject, final String text,
			final Map<String, byte[]> inlineAttachs) {
		logger.debug("Sending email to: " + to);

		// prepare message
		MimeMessagePreparator preparator = new MimeMessagePreparator() {
			public void prepare(MimeMessage mimeMessage) throws Exception {
				MimeMessageHelper message = new MimeMessageHelper(mimeMessage,
						true);
				message.setTo(to);
				message.setFrom(from, fromPersonal);
				message.setSubject(subject);
				message.setText(text, true);

				// attach images
				if (inlineAttachs != null && inlineAttachs.size() != 0) {
					Map.Entry<String, byte[]> image;
					for (Iterator<Map.Entry<String, byte[]>> it = inlineAttachs
							.entrySet().iterator(); it.hasNext();) {
						image = it.next();
						message.addInline(image.getKey(),
								new ByteArrayResource(image.getValue()) {
									@Override
									public String getFilename()
											throws IllegalStateException {
										return "noname";
									}
								});
					}
				}

			}
		};
		this.mailSender.send(preparator);
	}

	/**
	 * @see be.sc.classicnetwork.service.MailService#sendFreeMarkerTemplate(java.lang.String,
	 *      java.lang.String, java.lang.String, java.lang.String,
	 *      java.util.Collection, java.lang.String, java.lang.Object)
	 */
	public void sendFreeMarkerTemplate(String from, String fromPersonal,
			String to, String subject, Map<String, byte[]> inlineAttachs,
			String template, Object model) {
		try {
			// generate text from model
			String text = FreeMarkerTemplateUtils.processTemplateIntoString(
					configuration.getTemplate(template), model);
			send(from, fromPersonal, to, subject, text, inlineAttachs);
		} catch (IOException e) {
			throw new ApplicationException(e);
		} catch (TemplateException e) {
			throw new ApplicationException(e);
		}
	}

	public void setFreeMarkerConfigurer(
			FreeMarkerConfigurer freeMarkerConfigurer) {
		this.configuration = freeMarkerConfigurer.getConfiguration();
	}

	public JavaMailSender getMailSender() {
		return mailSender;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

}
